***
** MERGE MBR + PHUS ;

	libname unzipped "";
	libname zipped "";
	libname interm "";

	%MACRO unzipify;
		data _null_;
		   infile inzip lrecl=256 recfm=F length=length eof=eof unbuf;
		   file   ds lrecl=256 recfm=N;
		   input;
		   put _infile_ $varying256. length;
		   return;
		 eof: stop;
		run;
	%MEND;
	
	%LET zipstr = ;
	%LET memstr = mbr_seg02.sas7bdat;
	filename inzip ZIP "&zipstr" member="&memstr";
	filename ds "%sysfunc(getoption(work))/mbr.sas7bdat" ;
	%unzipify;

	data mbr; 
		set mbr (keep = can bic dob_: bdod_: sexprimary race doe: toc: doed:); 
	run;

	proc print data = mbr (obs = 30); where input(dob_ccyy,4.) = 1930 AND input(doei_tob,2.) = 13; run;

	proc freq data = mbr; where input(dob_ccyy,4.) = 1930; table doei_tob*toc1 / missing; table doei_tob*doec_tob / missing; run;





%MACRO mbrphus(SEG);

	%PUT ++ Merging for segment &SEG.;

****
** LOAD MBR;

	** Unzip raw MBR;
	%LET zipstr = ;
	%LET memstr = mbr_seg&SEG..sas7bdat;
	filename inzip ZIP "&zipstr" member="&memstr";
	filename ds "%sysfunc(getoption(work))/mbr.sas7bdat" ;
	%unzipify;

	data test (keep = doei_tob doec_tob); set mbr; run;
	proc freq data = test; title "Using DOEI_TOB"; table doei_tob*doec_tob / missing; run;

	** Keep key variables;
	data mbr (drop = dob_mm dob_dd dob_ccyy bdod_mm bdod_dd bdod_ccyy doei_mm doei_ccyy); 
		set mbr (keep = can bic dob_: bdod_: sexprimary race doei_: pia1); 
		dob = mdy(dob_mm, dob_dd, dob_ccyy);
		bdod = mdy(bdod_mm, bdod_dd, bdod_ccyy);
		doei = mdy(doei_mm, 1, doei_ccyy);
		format dob bdod doei date9.;
		tob = input(doei_tob, 2.);
	run;

	** Keep key cohorts from SER ;
	** Also keep only OA or DI cases (not rejected DI cases 13 or unknown 99);
	data mbr (drop = tob); set mbr; where year(dob) >= 1911 AND year(dob) <= 1950; where doei_tob = 1; *AND (tob = 1 OR tob = 2); doei_tob = tob; run;
																				

***
** LOAD PHUS;

	** Unzip raw PHUS;
	%LET zipstr = ;
	%LET memstr = phus_seg&SEG..sas7bdat;
	filename inzip ZIP "&zipstr" member="&memstr";
	filename ds "%sysfunc(getoption(work))/phus.sas7bdat" ;
	%unzipify;
	data phus; set phus (keep = CAN phus: ); run;   

	** Put PHUS in long form;
	proc transpose data = phus  out = phus; by CAN; run;
	proc sql noprint; 
		CREATE TABLE temp AS SELECT *, input(substr(_NAME_,5,2),4.) AS year, input(substr(_NAME_,7,2),2.) AS month 
			FROM phus WHERE COL1 ~= 0 ;
		CREATE TABLE phus AS SELECT can, COL1 AS phus, month, 
			(CASE WHEN year < 30 THEN year + 2000 WHEN year > 60 THEN year + 1900 ELSE . END) AS year
			FROM temp ORDER BY can, year, month;

	** Mark payments in first month and months 2-12;
	data phus; set phus; seq_id + 1; by can; if first.can then seq_id = 1; run;
	proc sql noprint;
		CREATE TABLE temp AS SELECT *,
			(CASE WHEN seq_id = 1 THEN phus ELSE . END) AS phus1,
			(CASE WHEN seq_id >= 2 AND seq_id <= 12 THEN phus ELSE . END) AS phus212
		FROM phus;

	** Collapse to person level;
	proc sql noprint;
		CREATE TABLE phus AS SELECT can, 
			min(mdy(month,1,year)) AS phus_entry format date9.,
			mean(phus1) AS phus1, mean(phus212) AS phus212
		FROM temp GROUP BY can;


****
** MERGE AND SAVE;

	* use a LEFT JOIN to keep only those with an mbr entry;
	proc sql noprint;
		CREATE TABLE interm.mbrphus&SEG. (compress = yes) 
		AS SELECT * FROM mbr A LEFT JOIN phus B ON A.can = B.can;

%MEND;

%mbrphus(02); %mbrphus(03); %mbrphus(05); %mbrphus(06); %mbrphus(09);
%mbrphus(10); %mbrphus(15); %mbrphus(16); %mbrphus(19); %mbrphus(20);
